<template>
  <!--登录-->
  <div class="sign-in">
    <img src="./../assets/logo.png" draggable="false">

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px">
      <el-form-item prop="uid">
        <el-input v-model="ruleForm.uid" placeholder="用户账号名"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="ruleForm.password" placeholder="用户密码"></el-input>
      </el-form-item>
      <el-form-item >
        <el-button type="primary" round size="medium" @click="signIn()">登&nbsp;&nbsp;&nbsp;录</el-button>
        <el-link :underline="false" href="/#/reset-p">忘记密码</el-link>|
        <el-link :underline="false" href="/#/sign-up">注册</el-link>|
        <el-link :underline="false" href="/#/share">分享</el-link>
      </el-form-item>
    </el-form>

    <div class="sign-in-power">
      © 2021 Powered by zsc ccying v1.0
    </div>
  </div>
</template>

<style>
  .sign-in {
    width: 600px;
    margin: auto;
  }
  .sign-in-power {
    padding-top: 100px;
  }
</style>

<script>
  export default {
    data() {
      return {
        ruleForm: {
          uid: '',
          password: ''
        },
        rules: {
          uid: [
            { required: true, message: '请输入用户账号名', trigger: 'blur' },
          ],
          password: [
            { required: true, message: '请输入用户密码', trigger: 'blur'},
            { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      //登录
      signIn() {
        let encodePassword = window.btoa(this.ruleForm.password);
        this.$http({
          method: 'post',
          url: '/',
          data: this.$qs.stringify({
            username: this.ruleForm.uid,
            password: encodePassword
          })
        }).then(res=>{
          let {code, message, data} = res.data;
          if (code === 200) {
            var user = JSON.stringify(data);
            sessionStorage.setItem('user', user);
            this.$router.push('/home');
          } else {
            alert(message);
          }
        })
      }
    }
  }
</script>
